const DataSource = {}
const commentChangeListener = []
const postChangeListener = []

const commentList = [
    {
        id: "0",
        comment: "this is good article!"
    },
    {
        id: "1",
        comment: "this really helps!thank you man!"
    },
    {
        id: "2",
        comment: "totally mess!"
    },
    {
        id: "3",
        comment: "What a wonder solution,oh"
    },
    {
        id: "4",
        comment: "a bit better than the old one!"
    },
    {
        id: '5',
        comment: "Can you help me doing this? I rarely get it"
    },
    {
        id: '6',
        comment: "may be this is the difference between the people,even bigger between dogs"
    },
    {
        id: '7',
        comment: "Hhahaaha,&#128514;"
    }
]
const postList = [
    { id: 0, post: "" },
    { id: 1, post: "" },
    { id: 2, post: "" },
    { id: 3, post: "" },
    { id: 4, post: "" },
    { id: 5, post: "" },
    { id: 6, post: "" },
    { id: 7, post: "" },
    { id: 8, post: "" },
]
DataSource.getComments = function () {
    return commentList
}

DataSource.getBlogPosts = function () {
    return postList
}

DataSource.handleOnCommentsChange = function () {
    console.log("handle on comments change" + commentChangeListener.length)
    commentChangeListener.forEach((v, index, arr) => { v() })
}

DataSource.changeComments = function () {
    console.log("change comments")
    commentList.forEach((v, index, arr) => {
        console.log(v+"==="+index)
        arr[index].comment = new Date().toLocaleDateString() + v.comment
    })
    this.handleOnCommentsChange()
}

DataSource.addOnCommentChangeListener = function (fn) {
    commentChangeListener.push(fn)
}
DataSource.removeOnCommentChangeListener = function (fn) {
    var idx = commentChangeListener.indexOf(fn)
    if (idx != -1) {
        commentChangeListener.splice(idx, 1)
    }
}

export default DataSource